package com.lance.shiro.mapper;

import com.lance.shiro.entity.IFeature;
import com.lance.shiro.entity.IPropertyList;
import com.lance.shiro.entity.IUser;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;

@Mapper
public interface PropertyListMapper {
    @Insert("insert into i_property_list " +
            "(name," +
            "buildingAddress," +
            "featureId," +
            "priceRange," +
            "suburb," +
            "city," +
            "state," +
            "country," +
            "developer," +
            "keyContact," +
            "buyerIncentive," +
            "x,y," +
            "buildingOverview," +
            "features," +
            "isFeatured," +
            "type," +
            "date," +
            "settlementDate," +
            "createTime," +
            "updateTime,status) " +
            "values(" +
            "#{name}," +
            "#{buildingAddress}," +
            "#{featureId}," +
            "#{priceRange}," +
            "#{suburb}," +
            "#{city}," +
            "#{state}," +
            "#{country}," +
            "#{developer}," +
            "#{keyContact}," +
            "#{buyerIncentive}," +
            "#{x},#{y}," +
            "#{buildingOverview}," +
            "#{features}," +
            "#{isFeatured}," +
            "#{type}," +
            "#{date}," +
            "now()," +
            "#{settlementDate}," +
            "now()," +
            "#{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public int add(IPropertyList propertyList);

    @Update("update i_property_list set name = #{name}," +
            "buildingAddress = #{buildingAddress}," +
            "featureId = #{featureId}," +
            "priceRange = #{priceRange}," +
            "suburb = #{suburb}," +
            "city = #{city}," +
            "state = #{state}," +
            "country = #{country}," +
            "developer = #{developer}," +
            "keyContact = #{keyContact}," +
            "buyerIncentive = #{buyerIncentive}," +
            "x = #{x},y = #{y}," +
            "buildingOverview = #{buildingOverview}," +
            "features = #{features}," +
            "isFeatured = #{isFeatured}," +
            "type = #{type}," +
            "date = #{date}," +
            "settlementDate = #{settlementDate}," +
            "status = #{status}," +
            "updateTime = now() " +
            "where id=#{id} ")
    int update(IPropertyList propertyList);

//    @Update("update i_property_list set ${propertyList}  ,updateTime = now() where id=#{id} ")
//    int update(@Param("id") int id, @Param("propertyList") String propertyList);

    @Select("select * from i_property_list where id=#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "lotTypeList", column = "id", javaType = List.class,
                    many = @Many(select = "com.lance.shiro.mapper.PropertyMapper.findAllLotType")),
            @Result(property = "featureId", column = "featureId"),
            @Result(property = "feature", column = "featureId", javaType = IFeature.class,
                    one = @One(select = "com.lance.shiro.mapper.FeatureMapper.fetchById"))
    })
    public IPropertyList get(int id);


    @Select("select id,name from i_property_list where id=#{id}")
    public IPropertyList getForPropertyGrid(int id);

    //根据city查询
    @Select("SELECT * FROM i_property_list where  city in (${citys})  ")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "featureId", column = "featureId"),
            @Result(property = "lotTypeList", column = "id", javaType = List.class,
                    many = @Many(select = "com.lance.shiro.mapper.PropertyMapper.findAllLotType"))
    })
    ArrayList<IPropertyList> findAllByCitys(@Param("citys") String citys);

    //查询所有用户
    //Zhan: Ignore column buildingOverview and features, return the rest of columns
   @Select("SELECT id, buildingAddress, city, state, country, x, y, date, status, createTime, updateTime, name, suburb, priceRange, featureId, type, settlementDate, isFeatured FROM i_property_list")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "featureId", column = "featureId"),
            @Result(property = "lotTypeList", column = "id", javaType = List.class,
                    many = @Many(select = "com.lance.shiro.mapper.PropertyMapper.findAllLotType")),
            @Result(property = "feature", column = "featureId", javaType = IFeature.class,
                            one = @One(select = "com.lance.shiro.mapper.FeatureMapper.fetchById"))

    })
    ArrayList<IPropertyList> findAll();

    @Update("delete from i_property_list where id=#{id} ")
    public void delete(int id);


    @Update("update i_property_list set ${attributes}  ,updateTime = now() where id=#{id} ")
    int updateAttribute(@Param("id") int id, @Param("attributes") String attributes);

    @Select("SELECT * FROM i_property_list where ${attributes}  ")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "lotTypeList", column = "id", javaType = List.class,
                    many = @Many(select = "com.lance.shiro.mapper.PropertyMapper.findAllLotType"))
    })
    ArrayList<IPropertyList> findAllByAttr(@Param("attributes") String attributes);

}
